/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Timer;
import java.util.TimerTask;
import main.Main;

/**
 *
 * @author Peter
 */
public class NinjaView implements PropertyChangeListener, AnimEventListener {
    
    
    Spatial ninja;
    Main app;
    private AnimChannel channel;
    private AnimControl control;
    private enum AnimState {stopped, walking}
    private AnimState state;
    Timer timer = new Timer();
    TimerTask stop = new TimerTask(){
             public void run() { 
                 channel.setAnim("Idle1");
                 channel.setLoopMode(LoopMode.DontLoop);
              }
           };
    public NinjaView(Main app) {
        this.app = app;
        this.ninja = this.app.getAssetManager().loadModel("Models/Ninja/Ninja.mesh.xml");
        ninja.scale(0.05f, 0.05f, 0.05f);
        this.app.getRootNode().attachChild(ninja);
        control = ninja.getControl(AnimControl.class);
        control.addListener(this);
        channel = control.createChannel();
        channel.setAnim("Idle1", 0.5f);
        channel.setLoopMode(LoopMode.DontLoop);
        
        //AnimChannel channel2 = control.createChannel();
        //channel2.setAnim("Idle2");
        //channel2.setLoopMode(LoopMode.Loop);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("turn")){
            ninja.setLocalRotation((Quaternion)evt.getNewValue());
        } else if (evt.getPropertyName().equals("walk")){
            ninja.setLocalTranslation((Vector3f)evt.getNewValue());
            if (channel.getAnimationName() != null && !channel.getAnimationName().equals("Walk")){
                channel.setAnim("Walk", 0.50f);
                channel.setLoopMode(LoopMode.Loop);
            }
            stop.cancel();
                stop = new TimerTask(){
                    public void run() { 
                        System.out.println(channel.getTime());
                         channel.setAnim("Idle1", 0.5f);
                         channel.setLoopMode(LoopMode.Loop);
                    }
                };
            timer.schedule(stop, 100);
        } else if (evt.getPropertyName().equals("backflip")){
             if (channel.getAnimationName() != null && !channel.getAnimationName().equals("Crouch")) {
                  channel.setAnim("Crouch", 0.50f);
                  channel.setLoopMode(LoopMode.DontLoop);
            }
        }
               
    }
    public float getAnimCycle(String anim){
        float result = 0;
        if (channel.getAnimationName() != null && channel.getAnimationName().equals(anim)){
            result = channel.getTime() / channel.getAnimMaxTime();
        }
        return result;
    }

    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
            if (channel.getAnimationName() != null && !channel.getAnimationName().equals("Walk")) {
                  channel.setAnim("Idle1", 0.50f);
                  channel.setLoopMode(LoopMode.Loop);
            }
    }

    public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
       
    }
    
}
